package com.partyjoin.project.system.dept.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.partyjoin.common.constant.UserConstants;
import com.partyjoin.common.utils.StringUtils;
import com.partyjoin.common.utils.security.ShiroUtils;
import com.partyjoin.project.system.dept.domain.Dept;
import com.partyjoin.project.system.dept.mapper.DeptMapper;
import com.partyjoin.project.system.user.domain.User;
import com.partyjoin.project.system.user.service.IUserService;

/**
 * 部门管理 服务实现
 * 
 * @author partyjoin
 */
@Repository("deptService")
public class DeptServiceImpl implements IDeptService
{
    @Autowired
    private DeptMapper deptMapper;
    
    @Autowired
    private IUserService userService;

    /**
     * 查询部门管理集合
     * 
     * @return 所有部门信息
     */
    @Override
    public List<Dept> selectDeptAll()
    {
        return deptMapper.selectDeptAll();
    }

    /**
     * 查询部门管理树
     * 
     * @return 所有部门信息
     */
    @Override
    public List<Map<String, Object>> selectDeptTree(Long deptId)
    {
        List<Map<String, Object>> trees = new ArrayList<Map<String, Object>>();
        List<Dept> deptList = deptMapper.selectDeptTree(deptId);

        for (Dept dept : deptList)
        {
            if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()))
            {
                Map<String, Object> deptMap = new HashMap<String, Object>();
                deptMap.put("id", dept.getDeptId());
                deptMap.put("pId", dept.getParentId());
                deptMap.put("name", dept.getDeptName());
                deptMap.put("title", dept.getDeptName());
                trees.add(deptMap);
            }
        }
        return trees;
    }

    /**
     * 查询部门人数
     * 
     * @param parentId 部门ID
     * @return 结果
     */
    @Override
    public int selectDeptCount(Long parentId)
    {
        Dept dept = new Dept();
        dept.setParentId(parentId);
        return deptMapper.selectDeptCount(dept);
    }

    /**
     * 查询部门是否存在用户
     * 
     * @param deptId 部门ID
     * @return 结果 true 存在 false 不存在
     */
    @Override
    public boolean checkDeptExistUser(Long deptId)
    {
        int result = deptMapper.checkDeptExistUser(deptId);
        return result > 0 ? true : false;
    }

    /**
     * 删除部门管理信息
     * 
     * @param deptId 部门ID
     * @return 结果
     */
    @Override
    public int deleteDeptById(Long deptId)
    {
        return deptMapper.deleteDeptById(deptId);
    }

    /**
     * 保存部门信息
     * 
     * @param dept 部门信息
     * @return 结果
     */
    @Override
    public int saveDept(Dept dept)
    {
        if (StringUtils.isNotNull(dept.getDeptId()))
        {
            dept.setUpdateBy(ShiroUtils.getLoginName());
            return deptMapper.updateDept(dept);
        }
        else
        {
            dept.setCreateBy(ShiroUtils.getLoginName());
            return deptMapper.insertDept(dept);
        }
    }

    /**
     * 根据部门ID查询信息
     * 
     * @param deptId 部门ID
     * @return 部门信息
     */
    @Override
    public Dept selectDeptById(Long deptId)
    {
        return deptMapper.selectDeptById(deptId);
    }

    /**
     * 校验部门名称是否唯一
     * 
     * @param dept 部门信息
     * @return 结果
     */
    @Override
    public String checkDeptNameUnique(Dept dept)
    {
        if (dept.getDeptId() == null)
        {
            dept.setDeptId(-1L);
        }
        Long deptId = dept.getDeptId();
        Dept info = deptMapper.checkDeptNameUnique(dept.getDeptName());
        if (StringUtils.isNotNull(info) && StringUtils.isNotNull(info.getDeptId())
                && info.getDeptId().longValue() != deptId.longValue())
        {
            return UserConstants.DEPT_NAME_NOT_UNIQUE;
        }
        return UserConstants.DEPT_NAME_UNIQUE;
    }
    
    /**
     * 根据登陆用户部门查询其部门及其子部门信息(查询各部门各角色人数及总人数)
     */
    public List<Dept> selectDeptMember(Long deptId){
    	List<Dept> deptList = deptMapper.selectDeptMember(deptId);
    	List<Dept> deptLists = new ArrayList<Dept>();//构造返回的部门数据
    	//把子部门各角色人数累计到相应父部门信息中
    	for(Dept dept : deptList){
    		dept.setUserName(userService.selectUserById(dept.getUserId()).getUserName());
    		//如果不是校党委
    		if(dept.getParentId() != 0){
    			//取父部门累加人数
    			for(int i = 0; i < deptList.size(); i++){
    				Dept d = deptList.get(i);
    				if(d.getDeptId() == dept.getParentId()){
    					//如果已存在此父部门，则先删除
    					if(deptLists.contains(d)){
    						deptLists.remove(d);
    					}
    					d.setActivistCount(d.getActivistCount() + dept.getActivistCount());
    					d.setDevelopCount(d.getDevelopCount() + dept.getDevelopCount());
    					d.setPrepareCount(d.getPrepareCount() + dept.getPrepareCount());
    					d.setOfficialCount(d.getOfficialCount() + dept.getOfficialCount());
    					d.setTotalCount(d.getTotalCount() + dept.getActivistCount() + dept.getDevelopCount() + dept.getPrepareCount() + dept.getOfficialCount());	
    					deptLists.add(d);
    				}
    			}
    		}
    		deptLists.add(dept);
    	}
    	//如果登录用户属于校党委，上面操作并没有累计人数到校党委部门，此处操作
    	if(deptList.get(0).getDeptId() == 100){
    		Dept dt = deptList.get(0);
    		//先移除校党委部门
    		deptLists.remove(dt);
    		for(Dept dept : deptLists){
    			if(dept.getParentId() == 100){
    				dt.setActivistCount(dt.getActivistCount() + dept.getActivistCount());
    				dt.setDevelopCount(dt.getDevelopCount() + dept.getDevelopCount());
    				dt.setPrepareCount(dt.getPrepareCount() + dept.getPrepareCount());
    				dt.setOfficialCount(dt.getOfficialCount() + dept.getOfficialCount());
    				dt.setTotalCount(dt.getTotalCount() + dept.getTotalCount());
    			}
    		}
    		deptLists.add(dt);
    	}
    	//如果登录用户属于二级部门，则加上校党委部门对象用于显示
    	if(deptList.get(0).getDeptId() != 100 && deptList.get(0).getParentId() == 100){
    		deptLists.add(selectDeptById(100l));
    	}
    	return deptLists;
    }

	@Override
	public String selectNameById(Long conferenceId) {
		return deptMapper.selectNameById(conferenceId);
	}

}
